package com.medical.manage.sync;

import com.medical.manage.domain.Search;
import com.medical.manage.es.SearchEs;
import com.medical.manage.esMapper.SearchEsMapper;
import com.medical.manage.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class SyncSearchEs implements ApplicationRunner {
    @Autowired
    private SearchService searchService;
    @Autowired
    private SearchEsMapper searchEsMapper;

    /**
     * 首页搜索数据
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //先查询要同步的数据
        List<Search> list = searchService.list();
        //创建新的集合
        ArrayList<SearchEs> arrayEsList = new ArrayList<>();
        //遍历要同步的数据
        list.forEach(item->{
            SearchEs searchEs = new SearchEs();
            BeanUtils.copyProperties(item,searchEs);
            arrayEsList.add(searchEs);
        });
        //批量添加到es
        searchEsMapper.insertBatch(arrayEsList);
        log.info("首页搜索数据同步成功");
    }
}
